﻿// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using OpenNETCF.Win32.Notify;

namespace svs.Mobile.BeyondScheduler.Core.Objects
{
	internal class SchedulingMethodRunApplicationAtTime : ISchedulingMethod
	{
		#region ISchedulingMethod Members

		public void UpdateScheduledTasks(Dictionary<string, ScheduledTask> tasks)
		{
			foreach (ScheduledTask task in tasks.Values)
			{
				if (task.Active && task.Event.StartTime > DateTime.Now)
				{
					task.Event.ActualStartTime = task.Event.StartTime;
				}
			}
		}

		public void DeleteScheduledTaskFromDevice(ScheduledTask task)
		{
			CoreHelper.WriteLogEntry(string.Format("Deleting scheduled run for Task: {0}", task));

			//Delete only if we have only one task left
			if (TaskRepository.ActiveTaskCount == 0)
			{
				try
				{
					Notify.RunAppAtTime(task.Event.ApplicationPath, DateTime.MinValue);
				}
				catch
				{
					CoreHelper.WriteLogEntry(
						string.Format("Unable to clear RunAppAtTime"));
				}
			}

			task.Event.EventHandle = 0;
			task.Event.ActualStartTime = null;
		}

		public void SetScheduledTaskToDevice(ScheduledTask task)
		{
			if (task.Event == null)
				throw new ArgumentException("Task event can't be null!");

			if (!task.Active)
			{
				CoreHelper.WriteLogEntry(
					string.Format("Task {0} is INACTIVE!", task));
				return;
			}

			if (!task.Event.OccursInTheFuture)
			{
				CoreHelper.WriteLogEntry(
					string.Format("Task {0} has no Recurrence!", task));
				return;
			}

			//Because with RunAppAtTime you can schedule only one task a time, here we have to schedule whatever the next task is
			ScheduledTask NextTask = TaskRepository.GetFirstTaskToExecuteAfter(DateTime.Now);

			try
			{
				Notify.RunAppAtTime(NextTask.Event.ApplicationPath, NextTask.Event.StartTime.Value);

				//Now update our task to indicate that it was scheduled correctly
				task.Event.EventHandle = 0;
				task.Event.ActualStartTime = task.Event.StartTime;
				task.Event.UnadjustedStartTime = task.Event.StartTime;

				CoreHelper.WriteLogEntry(
					string.Format("Scheduled task: {0}, (Next task to execute: {1})", task, NextTask));
			}
			catch
			{
				CoreHelper.WriteLogEntry(
					string.Format("Unable to set Notification For: {0}", NextTask));
			}
		}

		#endregion
	}
}